package com.wawayaya.bookreader.worddict;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

//  'WordDictdescData' is for one dictionary; for multiple dictionaries, to array 'WordDictdescData'
public class WordDictdescData implements Parcelable {

	/*public final static int ms_iworddictdesc_datatype_none = -1;
	public final static int ms_iworddictdesc_datatype_full = 1;
	public final static int ms_iworddictdesc_datatype_onlytext = 2;
	public final static int ms_iworddictdesc_datatype_onlyimage = 3;
	public final static int ms_iworddictdesc_datatype_imagefail = 4;*/

    public int m_iviewId = -1;
    //public int m_iresulttype = ms_iworddictdesc_datatype_none;

    public int m_inetid = -1;
    public int m_ibookid = -1;
    public String m_sdictname = null;
    public String m_sbaseword = null;
    public String m_swordpinyin = null;
    //public String m_spriorityimgurl = null;
    //public WordDictdescItem[] m_items = null;
    public String[] m_ssduyin = null;
    public WordOneDictData[]  m_dicts = null;

    public void clear() {
        if(null != m_dicts) {
            for(WordOneDictData item : m_dicts) {
                if(null != item) {
                    item.clear();
                }
            }
        }
    }
	
	/*public WordDictdescData cloneimagepart() {
		WordDictdescData ret = new WordDictdescData();
		ret.m_iviewId = m_iviewId;
		//ret.m_iresulttype = ms_iworddictdesc_datatype_onlyimage;
		ret.m_inetid = m_inetid;
		ret.m_ibookid = m_ibookid;
		ret.m_sdictname = m_sdictname;
		ret.m_sbaseword = m_sbaseword;
		ret.m_swordpinyin = m_swordpinyin;
		ret.m_spriorityimgurl = m_spriorityimgurl;
		if(TextUtils.isEmpty(m_spriorityimgurl)) {
			return ret;
		}
		ret.m_spriorityimgurl = ret.m_spriorityimgurl.trim();
		String[] simgidxs = ret.m_spriorityimgurl.split(" ");
		if(null == simgidxs) {
			return ret;
		}
		if(null == m_items) {
			return ret;
		}		
		
		// [{"items":[{"fan_yi_ci":[],"pin_yin":"mao1","pei_tu":[],"jie_shi":[{"str":"cat","li_ju":[],"duan_yu":[]}]},{"fan_yi_ci":[],"pin_yin":"mao2","pei_tu":["http:\/\/10.135.195.180:8086\/ft_image\/gif_187911_6347.jpg"],"jie_shi":[{"str":"另见 (mao1)","li_ju":[],"duan_yu":[]}]}],"name":"通用词典"}]
		if(simgidxs.length > m_items.length) {
			return ret;
		}
		ret.m_items = new WordDictdescItem[m_items.length];
		for(int i = 0; i < ret.m_items.length; i++){
			ret.m_items[i] = null;
		}
		for(int i = 0; i < simgidxs.length; i++) {
			int idx = Integer.parseInt(simgidxs[i]);
			ret.m_items[idx] = new WordDictdescItem();
			try{
				ret.m_items[idx].m_itemId = idx;
				ret.m_items[idx].m_sdictimageurl = m_items[ret.m_items[idx].m_itemId].m_sdictimageurl;
				ret.m_items[idx].m_bmpimage = m_items[ret.m_items[idx].m_itemId].m_bmpimage;
			}catch(Exception e){
				ret.m_items[idx].m_itemId = -1;
				ret.m_items[idx].m_sdictimageurl = null;
				ret.m_items[idx].m_bmpimage = null;
			}
		}
		return ret;
	}*/

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(m_iviewId);
        //dest.writeInt(m_iresulttype);
        dest.writeInt(m_inetid);
        dest.writeInt(m_ibookid);
        dest.writeString(m_sdictname);
        dest.writeString(m_sbaseword);
        dest.writeString(m_swordpinyin);
        //dest.writeString(m_spriorityimgurl);
        if(null != m_dicts) {
            dest.writeInt(m_dicts.length);
            dest.writeTypedArray(m_dicts, 0);
        }
        else{
            dest.writeInt(0);
        }
    }

    public static final Parcelable.Creator<WordDictdescData> CREATOR = new Creator<WordDictdescData>(){

        @Override
        public WordDictdescData createFromParcel(Parcel source) {
            WordDictdescData pwdd = new WordDictdescData();
            pwdd.m_iviewId = source.readInt();
            //pwdd.m_iresulttype = source.readInt();
            pwdd.m_inetid = source.readInt();
            pwdd.m_ibookid = source.readInt();
            pwdd.m_sdictname = source.readString();
            pwdd.m_sbaseword = source.readString();
            pwdd.m_swordpinyin = source.readString();
            //pwdd.m_spriorityimgurl = source.readString();
            int lenght = source.readInt();
            if(lenght <= 0) {
                pwdd.m_dicts = null;
            }
            else{
                pwdd.m_dicts = new WordOneDictData[lenght];
                source.readTypedArray(pwdd.m_dicts, WordOneDictData.CREATOR);
            }
            return pwdd;
        }

        @Override
        public WordDictdescData[] newArray(int size) {
            return new WordDictdescData[size];
        }
    };

	/* ===================== */

    public static class WordDictdescItem implements Parcelable {

        public int m_itemId = -1;
        public String m_spinyin = null;
        public WordDictdescItemJieShi[] m_jieshis = null;
        public String m_sdictimageurl = null;
        public Bitmap m_bmpimage = null;


        public void clear() {
            if(null != m_bmpimage) {
                try{
                    m_bmpimage.recycle();
                }catch(Exception e){
                }
                m_bmpimage = null;
            }
            m_jieshis = null;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(m_itemId);
            dest.writeString(m_spinyin);
            if(null == m_jieshis) {
                dest.writeInt(0);
            }
            else{
                dest.writeInt(m_jieshis.length);
                dest.writeTypedArray(m_jieshis, 0);
            }
            dest.writeString(m_sdictimageurl);
            if(!TextUtils.isEmpty(m_sdictimageurl)) {
                if(null != m_bmpimage) {
                    m_bmpimage.writeToParcel(dest, 0);
                }
            }
        }

        public static final Parcelable.Creator<WordDictdescItem> CREATOR = new Creator<WordDictdescItem>() {

            @Override
            public WordDictdescItem createFromParcel(Parcel source) {
                WordDictdescItem pwdi = new WordDictdescItem();
                pwdi.m_itemId = source.readInt();
                pwdi.m_spinyin = source.readString();
                int lenght = source.readInt();
                if(lenght <= 0) {
                    pwdi.m_jieshis = null;
                }
                else{
                    pwdi.m_jieshis = new WordDictdescItemJieShi[lenght];
                    source.readTypedArray(pwdi.m_jieshis, WordDictdescItemJieShi.CREATOR);
                }
                pwdi.m_sdictimageurl = source.readString();
                pwdi.m_bmpimage = null;
                if(!TextUtils.isEmpty(pwdi.m_sdictimageurl)) {
                    try{
                        pwdi.m_bmpimage = Bitmap.CREATOR.createFromParcel(source);
                    }catch(Exception e){
                    }
                }
                return pwdi;
            }

            @Override
            public WordDictdescItem[] newArray(int size) {
                return new WordDictdescItem[size];
            }
        };

    }


	/* ===================== */

    public static class WordDictdescItemJieShi implements Parcelable {

        public String m_sshiyi = null;
        public ParcelableCizu[] m_lijus = null;
        public ParcelableCizu[] m_cizus = null;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(m_sshiyi);
            if(null == m_lijus) {
                dest.writeInt(0);
            }
            else{
                dest.writeInt(m_lijus.length);
                dest.writeTypedArray(m_lijus, 0);
            }
            if(null == m_cizus) {
                dest.writeInt(0);
            }
            else{
                dest.writeInt(m_cizus.length);
                dest.writeTypedArray(m_cizus, 0);
            }
        }

        public static final Parcelable.Creator<WordDictdescItemJieShi> CREATOR = new Creator<WordDictdescItemJieShi>(){

            @Override
            public WordDictdescItemJieShi createFromParcel(Parcel source) {
                WordDictdescItemJieShi wdij = new WordDictdescItemJieShi();
                wdij.m_sshiyi = source.readString();
                int lenght = source.readInt();
                if(lenght <= 0) {
                    wdij.m_lijus = null;
                }
                else{
                    wdij.m_lijus = new ParcelableCizu[lenght];
                    source.readTypedArray(wdij.m_lijus, ParcelableCizu.CREATOR);
                }
                lenght = source.readInt();
                if(lenght <= 0) {
                    wdij.m_cizus = null;
                }
                else{
                    wdij.m_cizus = new ParcelableCizu[lenght];
                    source.readTypedArray(wdij.m_cizus, ParcelableCizu.CREATOR);
                }
                return wdij;
            }

            @Override
            public WordDictdescItemJieShi[] newArray(int size) {
                return new WordDictdescItemJieShi[size];
            }

        };

    }

	/* ===================== */

    public static class ParcelableCizu implements Parcelable {

        public String m_cizu;
        public String m_cizujieshi;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(m_cizu);
            dest.writeString(m_cizujieshi);
        }

        public static final Parcelable.Creator<ParcelableCizu> CREATOR = new Creator<ParcelableCizu>() {

            @Override
            public ParcelableCizu createFromParcel(Parcel source) {
                ParcelableCizu pc = new ParcelableCizu();
                pc.m_cizu = source.readString();
                pc.m_cizujieshi = source.readString();
                return pc;
            }

            @Override
            public ParcelableCizu[] newArray(int size) {
                return new ParcelableCizu[size];
            }

        };

    }

    public static class WordOneDictData implements Parcelable {

        public int m_dictId = -1;
        public String m_spinyin = null;
        public String m_sdictimageurl = null;
        public Bitmap m_bmpimage = null;
        public String m_type = null;
        public WordDictdescItem[] m_items = null;

        public void clear() {
            if(null != m_bmpimage) {
                try{
                    m_bmpimage.recycle();
                }catch(Exception e){
                }
                m_bmpimage = null;
            }
            if(null != m_items) {
                for(WordDictdescItem item : m_items) {
                    if(null != item) {
                        item.clear();
                    }
                }
            }

        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(m_dictId);
            dest.writeString(m_spinyin);
/*            if(null == m_jieshis) {
                dest.writeInt(0);
            }
            else{
                dest.writeInt(m_jieshis.length);
                dest.writeTypedArray(m_jieshis, 0);
            }*/
            dest.writeString(m_sdictimageurl);
            if(!TextUtils.isEmpty(m_sdictimageurl)) {
                if(null != m_bmpimage) {
                    m_bmpimage.writeToParcel(dest, 0);
                }
            }
            dest.writeString(m_type);
            if(null == m_items) {
                dest.writeInt(0);
            }
            else{
                dest.writeInt(m_items.length);
                dest.writeTypedArray(m_items, 0);
            }
        }

        public static final Parcelable.Creator<WordOneDictData> CREATOR = new Creator<WordOneDictData>() {

            @Override
            public WordOneDictData createFromParcel(Parcel source) {
                WordOneDictData pwdi = new WordOneDictData();
                pwdi.m_dictId = source.readInt();
                pwdi.m_spinyin = source.readString();
                /*int lenght = source.readInt();
                if(lenght <= 0) {
                    pwdi.m_jieshis = null;
                }
                else{
                    pwdi.m_jieshis = new WordDictdescItemJieShi[lenght];
                    source.readTypedArray(pwdi.m_jieshis, WordDictdescItemJieShi.CREATOR);
                }*/
                pwdi.m_sdictimageurl = source.readString();
                pwdi.m_bmpimage = null;
                if(!TextUtils.isEmpty(pwdi.m_sdictimageurl)) {
                    try{
                        pwdi.m_bmpimage = Bitmap.CREATOR.createFromParcel(source);
                    }catch(Exception e){
                    }
                }
                pwdi.m_type = source.readString();
                int lenght = source.readInt();
                if(lenght <= 0) {
                    pwdi.m_items = null;
                }
                else{
                    pwdi.m_items = new WordDictdescItem[lenght];
                    source.readTypedArray(pwdi.m_items, WordDictdescItem.CREATOR);
                }
                return pwdi;
            }

            @Override
            public WordOneDictData[] newArray(int size) {
                return new WordOneDictData[size];
            }
        };
    }

}
